from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
from django.shortcuts import render
from django.urls import reverse
from . import models
from . import service
import json
# Create your views here.
def showlist(request):
    from django.forms.models import model_to_dict
    all_news=models.News.objects.all()
    d=[]
    for i in all_news:
        d.append(model_to_dict(i))
    return JsonResponse(d,safe=False)
def add1(request):
    news=json.loads(request.body)
    """ for var in list:
        if news['Title']!=var.Title:
            news1=models.News(Title=news['Title'],Content=news['Content'])
            news1.save()
            ret={'code':0,'msg':'记录成功'}
        elif news['Title']==var.Title:
            news1=models.News(Title=news['Title'],Content=news['Content'])
            news1.save()
            ret={'code':1,'msg':'修改成功'}
    return HttpResponse(json.dumps(ret)) """
    news1=models.News(Title=news['Title'],Content=news['Content'])
    news1.save()
    ret={'code':1,'msg':'修改成功'}
    return HttpResponse(json.dumps(ret))
def add(request):
    return render(request,'news_list/add.html',{})
def svc_add(request):
    Title=request.POST["Title"]
    Content=request.POST["Content"]
    service.addPoem(Title,Content)
    return HttpResponse('后台上传成功')